<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 实体控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\GlobalNoticeService;
use common\service\ContractService;
use common\validate\GlobalNotice;
use think\Request;
use support\ErrorCode;
use think\Response;

class GlobalNoticeController extends VerifyController
{

    /**
     * @var \common\service\GlobalNoticeService
     */
    private GlobalNoticeService $globalNoticeService;

    public function __construct()
    {
        parent::__construct();
        $this->globalNoticeService = GlobalNoticeService::getInstance();
     }


    /**
     * 发布全局公告
     * @param Request $request
     * @return Response|void
     */
     public function publish(Request $request)
     {
         $param = $request->all();
         $param = $param['param'] ?? [];
         $validate = new GlobalNotice();
         $validate->scene('Create');

         if (!$validate->check($param)) {
             throw_strack_exception($validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
         }

         if (empty($param['data']['global_notice'])) {
             throw_strack_exception("missing global notice data", ErrorCode::REQUEST_PARAM_ERROR);
         }

         try {
             $req = $this->globalNoticeService->publish($request->getUserId(), $param['data']);
             return json(success_response('', $req));
         } catch (LogicException $e) {
             throw_strack_exception("publish notice failed", $e->getCode());
         }
     }

    /**
     * 发布公告评论
     * @param Request $request
     * @return Response|void
     */
     public function comment(Request $request)
     {
         $param = $request->all();
         $param = $param['param'] ?? [];

         if (!isset($param['data']['comment']) || empty($param['data']['comment'])) {
             throw_strack_exception("missing comment param", ErrorCode::REQUEST_PARAM_ERROR);
         }

         try {
             $req = $this->globalNoticeService->comment($request->getUserId(), $param['data']);
             return json(success_response('', $req));
         } catch (LogicException $e) {
             throw_strack_exception("publish notice comment failed", $e->getCode());
         }
     }

    /**
     * 查询公告详情
     * @param Request $request
     * @return Response|void
     */
    public function getContent(Request $request)
    {
        $param = $request->param()['param'];
        $queryType = $param['fields'] ?? '';
        $globalNoticeId = $param['filter']['id'] ?? 0;
        if ($queryType !== 'content' || empty($globalNoticeId)) {
            throw_strack_exception('not found', ErrorCode::REQUEST_PARAM_ERROR);
        }

        try {
            $req = $this->globalNoticeService->getContent($globalNoticeId);
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception("get notice content failed", $e->getCode());
        }
    }

    /**
     * 全局公告统计
     * @param Request $request
     * @return Response|void
     */
    public function getCount(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $res = $this->globalNoticeService->getCount($param, $request->getUserId());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 公告列表
     * @param Request $request
     * @return Response|void
     */
    public function list(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $res = $this->globalNoticeService->list($param, $request->getUserId());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 确认全局公告
     * @param Request $request
     * @return Response|void
     */
    public function confirm(Request $request)
    {
        $param = $request->all();
        $param = $param['param'] ?? [];

        if (!isset($param['data']['notice_id']) || empty($param['data']['notice_id'])) {
            throw_strack_exception("missing global notice id", ErrorCode::REQUEST_PARAM_ERROR);
        }

        try {
            $req = $this->globalNoticeService->confirm($param['data']['notice_id'], $request->getUserId());
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception("confirm global notice failed", $e->getCode());
        }
    }

    /**
     * 删除全局公告
     * @param Request $request
     * @return Response|void
     */
    public function abolish(Request $request)
    {
        $param = $request->all();
        $param = $param['param'] ?? [];

        if (!isset($param['data']['notice_id']) || empty($param['data']['notice_id'])) {
            throw_strack_exception("missing global notice id", ErrorCode::REQUEST_PARAM_ERROR);
        }

        try {
            $req = $this->globalNoticeService->abolish($param['data']['notice_id'], $request->getUserId());
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception("abolish global notice failed", $e->getCode());
        }
    }

    /**
     * 查询公告详情
     * @param Request $request
     * @return Response|void
     */
    public function view(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $data = $param['data'] ?? [];
            $noticeId = intval($data['id'] ?? 0);

            if (empty($noticeId)) {
                throw_strack_exception("missing global notice id", ErrorCode::REQUEST_PARAM_ERROR);
            }

            $res = $this->globalNoticeService->view($noticeId, $request->getUserId());
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查询最近提到的公告用户列表
     * @param Request $request
     * @return Response|void
     */
    public function recentMentionedUser(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->globalNoticeService->recentMentionedUser($param, $request->getUserId());
        return json(success_response('OK', $res));
    }

    /**
     * 公告类型option列表
     * @param Request $request
     * @return Response|void
     */
    public function noticeOptionList(Request $request)
    {
        $res = $this->globalNoticeService->noticeTypeOptionData();
        return json(success_response('OK', $res));
    }

    /**
     * 公告提醒
     * @param Request $request
     * @return Response|void
     */
    public function notify(Request $request)
    {
        $req = $this->globalNoticeService->notify(intval($request->getUserId()));
        return json(success_response('', $req));
    }

    /**
     * 删除公告评论
     * @param Request $request
     * @return Response|void
     */
    public function delComment(Request $request)
    {
        $param = $request->param()['param'];
        $data = $param['data'] ?? [];
        $commentId = intval($data['comment_id'] ?? 0);
        $noticeId = intval($data['notice_id'] ?? 0);
        $userId = intval($request->getUserId());

        $req = $this->globalNoticeService->delComment($noticeId, $commentId, $userId);

        try {
            return json(success_response('', $req));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查看公告提到的人员列表
     * @param Request $request
     * @return Response|void
     */
    public function noticeMentionUsers(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $data = $param['data'] ?? [];
            $noticeId = intval($data['id'] ?? 0);
            $isConfirmed = intval($data['is_confirmed'] ?? 0);
            $page = $param['page'] ?? [1, C('default_page_total')];

            if (empty($noticeId)) {
                throw_strack_exception("missing global notice id", ErrorCode::REQUEST_PARAM_ERROR);
            }

            $res = $this->globalNoticeService->noticeMentionUsers($noticeId, $isConfirmed, $page);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 公告评论列表
     * @param Request $request
     * @return Response|void
     */
    public function noticeComments(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $data = $param['data'] ?? [];
            $noticeId = intval($data['id'] ?? 0);
            $page = $param['page'] ?? [1, C('default_page_total')];

            if (empty($noticeId)) {
                throw_strack_exception("missing global notice id", ErrorCode::REQUEST_PARAM_ERROR);
            }

            $res = $this->globalNoticeService->noticeComments($noticeId, $page);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 公告附件列表
     * @param Request $request
     * @return Response|void
     */
    public function noticeMediaList(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $data = $param['data'] ?? [];
            $noticeId = intval($data['id'] ?? 0);

            if (empty($noticeId)) {
                throw_strack_exception("missing global notice id", ErrorCode::REQUEST_PARAM_ERROR);
            }

            $res = $this->globalNoticeService->noticeMediaList($noticeId);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}
